package com.gjy.minio.controller;

import com.gjy.minio.domain.AjaxResult;
import com.gjy.minio.domain.BucketInfo;
import com.gjy.minio.domain.FileInfo;
import com.gjy.minio.domain.vo.BucketRequest;
import com.gjy.minio.domain.vo.BucketResponse;
import com.gjy.minio.service.MinioService;
import com.google.common.collect.Lists;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-09-04 20:08:29
 */
@RestController
@RequestMapping("/minio")
public class MinioController {

    @Resource
    private MinioService minioService;

    private static final List<String> BUCKET_OPERATIONS = Lists.newArrayList("exists", "create", "delete", "delete_force", "get");

    @PostMapping("/bucket")
    public AjaxResult bucket(@RequestBody BucketRequest request) {
        AjaxResult r;
        BucketResponse response = new BucketResponse();
        boolean bucket;
        switch (request.getOperation()) {
            case "exists":
                bucket = minioService.existsBucket(request.getBucket());
                response.setResult(bucket);
                r = AjaxResult.builder().code(200).message("OK").data(response).build();
                break;
            case "create":
                bucket = minioService.createBucket(request.getBucket(), request.getType());
                response.setResult(bucket);
                r = AjaxResult.builder().code(200).message("OK").data(response).build();
                break;
            case "delete":
                bucket = minioService.deleteBucket(request.getBucket());
                response.setResult(bucket);
                r = AjaxResult.builder().code(200).message("OK").data(response).build();
                break;
            case "delete_force":
                bucket = minioService.deleteBucketForce(request.getBucket());
                response.setResult(bucket);
                r = AjaxResult.builder().code(200).message("OK").data(response).build();
                break;
            case "get":
                List<BucketInfo> buckets = minioService.getBuckets();
                response.setResult(buckets);
                r = AjaxResult.builder().code(200).message("OK").data(response).build();
                break;
            default:
                r = AjaxResult.builder().code(400).message("参数错误,operator仅支持: " + BUCKET_OPERATIONS).build();
        }
        return r;
    }

    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestPart String bucket,
                                 @RequestPart MultipartFile file) {
        FileInfo info = minioService.uploadFile(bucket, file);
        return AjaxResult.builder().code(200).message("OK").data(info).build();
    }

    @PostMapping("/uploads")
    public AjaxResult uploadFiles(@RequestPart String bucket,
                                  @RequestPart List<MultipartFile> files) {
        List<FileInfo> info = minioService.uploadFile(bucket, files);
        return AjaxResult.builder().code(200).message("OK").data(info).build();
    }

    @PostMapping("/preview")
    public AjaxResult previewFile(@RequestParam String bucket,
                                  @RequestParam String filename) {
        FileInfo info = minioService.previewFile(bucket, filename);
        return AjaxResult.builder().code(200).message("OK").data(info).build();
    }

    @PostMapping("/delete")
    public AjaxResult deleteFile(@RequestParam String bucket,
                                 @RequestParam String filename) {
        boolean info = minioService.deleteFile(bucket, filename);
        return AjaxResult.builder().code(200).message("OK").data(info).build();
    }

    @PostMapping("/download")
    public void downloadFile(@RequestParam String bucket,
                             @RequestParam String filename,
                             HttpServletResponse response) {
        minioService.downloadFile(bucket, filename, response);
    }

}
